<?php

namespace App\Admin;

use Illuminate\Database\Eloquent\Model;
use App\Admin\Auth;

class Role extends Model
{
    //定义关联的数据表
    protected $table = 'role';
    //禁用时间字段
    public $timestamps = false;

    public function assignment($data) {
        $Authid = Auth::where('pid', '>', 0) -> get();
        $auth_ac = '';
        if(!isset($data['auth_ids'])) {
            $data['auth_ids'] = [];
        }
        // $data['auth_ids'] = $data['auth_ids'] ? $data['auth_ids'] : [];
        foreach($Authid as $value) {
            if(in_array($value -> id, $data['auth_ids'])) {
                $auth_ac .= $value -> controller.'@'.$value -> action.',';
            }
        }
        $data['auth_ac'] = rtrim($auth_ac,',');
        $data['auth_ids'] = implode(',', $data['auth_ids']);
        return $result = Role::where('id', $data['id']) -> update($data);
    }
    //添加角色
    public function addrole($data) {
        $Authid = Auth::where('pid', '>', 0) -> get();
        $auth_ac = '';
        foreach($Authid as $value) {
            if(in_array($value -> id, $data['auth_ids'])) {
                $auth_ac .= $value -> controller.'@'.$value -> action.',';
            }
        }
        $data['auth_ac'] = rtrim($auth_ac,',');
        $data['auth_ids'] = implode(',', $data['auth_ids']);
        $result = Role::insert($data);
        return $result ? 1 : 0;
    }
}
